package cn.vacuumflask.cordova;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.dascom.print.SmartPrint;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import cn.vacuumflask.cordova.utils.L;

/**
 * Created by Administrator on 2018/3/8 0008.
 */

public class JavaScriptEvent extends CordovaPlugin {

    private ProgressDialog progressDialog;
    private ContactInfo contactInfo;

    private Activity act;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                String readdate = msg.getData().getString("READDATE");
                L.d("连接状态返回：" + readdate);
                if (readdate == null) {

                    Toast.makeText(act, "打印失败", Toast.LENGTH_LONG).show();

                } else if (readdate.equals("0")) {//正加连接
                    if (progressDialog != null) {
                        progressDialog.show();
                    }

                } else if (readdate.equals("2")) {//正加打印
                    L.d("进行打印");
                    //打印
                    if (contactInfo != null) {
                        contactInfo.printBill();
                    } else {
                        if (smartPrint != null) {
                            smartPrint.DSCloseWifi();
                        }
                    }

                } else if (readdate.contains("3") && readdate.contains("Exception")) {//打印连接失败

                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }

                    if (readdate.contains("Connection reset by peer")) {
                        Toast.makeText(act, "打印机忙，请稍后重试", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(act, "打印机连接失败", Toast.LENGTH_LONG).show();
                    }

                }
            }
        }
    };
    private SmartPrint smartPrint;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        //初始化
        init();

        L.d("点击事件：" + action);


        if ("printOrder".equals(action)) {

            String content = args.getString(0);
            L.d("json：" + content);

            boolean printOrder = contactInfo.printOrder(content);

            if (!printOrder) {
                Toast.makeText(act, "打印失败，数据为空", Toast.LENGTH_LONG).show();
            }
            return true;
        }
        return false;
    }


    private void init() {
        if (act == null) {
            act = this.cordova.getActivity();
        }
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(act);
            progressDialog.setMessage("正在打印，请稍后...");
            progressDialog.setCancelable(false);
        }
        if (smartPrint == null) {
            smartPrint = new SmartPrint(act, handler, 2);
        }

        if (contactInfo == null) {
            contactInfo = new ContactInfo(act, smartPrint, progressDialog);
        }
    }


}
